<?php
declare(strict_types=1);

namespace Modules\Inventory\Http\Controllers;

use Catch\Base\CatchController as Controller;
use Modules\Base\Repository\MaterialRepository;
use Modules\Inventory\Models\Inventory;
use Illuminate\Http\Request;use Modules\Inventory\Repository\InventoryRepository;
use App\Services\InventoryService;
use Illuminate\Support\Facades\DB;


class InventoryController extends Controller
{
    public function __construct(
        protected readonly Inventory $model
    ){}

    /**
     * @param Request $request
     * @return mixed
     */
    public function index(Request $request): mixed
    {
        return InventoryRepository::getList($request->all());
    }

    /**
     * @param Request $request
     * @return mixed
     */
    public function store(Request $request)
    {
        return $this->model->storeBy($request->all());
    }

    /**
     * @param $id
     * @return mixed
     */
    public function show($id)
    {
        return $this->model->firstBy($id);
    }

    /**
     * @param Request $request
     * @param $id
     * @return mixed
     */
    public function update($id, Request $request)
    {
        return $this->model->updateBy($id, $request->all());
    }

    /**
     * @param $id
     * @return mixed
     */
    public function destroy($id)
    {
        return $this->model->deleteBy($id);
    }


    /* 库存重算
     * @param $id
     * @return mixed
     */
    public function rollback(Request $request)
    {
        $data = $request->all();
        try {
            //开始起事务
           DB::beginTransaction();
            foreach($data as $k=>$v){
                InventoryRepository::resetInventory($v['org_code'],$v['cargo_code'],$v['material_no'],$v['lot_num'],$v['location']);
            }
           DB::commit();
           return true;
       } catch (\Exception $e) {
           DB::rollBack();
           throw new FailedException($e);
       }       
    }



}
